//
//  AlreadyApplicationCell.swift
//  QualityControlSystem
//
//  Created by dundun on 17/1/18.
//  Copyright © 2017年 com.mask. All rights reserved.
//

import UIKit


class AlreadyApplicationCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
    
    var collectionViewCell: UICollectionView?
    var selectApplicationBlock: ((_ model: ApplicationModel) -> Void)?
    var data: [ApplicationModel]? {
        didSet {
            collectionViewCell?.reloadData()
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        setupCollectionView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupCollectionView() {
        let collectionViewLayout = UICollectionViewFlowLayout()
        collectionViewLayout.itemSize = CGSize(width: 95, height: 120)
        collectionViewLayout.scrollDirection = .horizontal
        collectionViewLayout.minimumLineSpacing = Padding.space
        collectionViewLayout.minimumInteritemSpacing = Padding.space
        collectionViewLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)

        collectionViewCell = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0), collectionViewLayout: collectionViewLayout)
        collectionViewCell?.alwaysBounceVertical = false
        collectionViewCell?.alwaysBounceHorizontal = true
        collectionViewCell?.delegate = self
        collectionViewCell?.dataSource = self
        collectionViewCell?.scrollsToTop = false
        collectionViewCell?.showsVerticalScrollIndicator = false
        collectionViewCell?.showsHorizontalScrollIndicator = false
        collectionViewCell?.backgroundColor = Color.bgGray
        collectionViewCell?.register(UINib(nibName: "AlreadyCell", bundle: nil), forCellWithReuseIdentifier: "alreadyCell")
        
        contentView.addSubview(collectionViewCell!)
        collectionViewCell?.snp.makeConstraints({ (make) in
            make.top.left.bottom.right.equalTo(self)
        })
    }
    
    // MARK: - DataSource
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "alreadyCell", for: indexPath) as? AlreadyCell
        let model = (data?[indexPath.row])! as ApplicationModel
        cell?.cellImageView.sd_setImage(with: URL(string: model.iconurl!), placeholderImage: UIImage(named: "placeholderImage.jpg"))
        cell?.cellTitle.text = model.text
        return cell!
    }
    
    // MARK: - Delegate
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = (data?[indexPath.row])! as ApplicationModel
        selectApplicationBlock!(model)
    }

}
